$.extend($.fn.validatebox.defaults.rules, {
    date: {
        validator: function (value)
        {
            return isDate(value);
        },
        message: '请输入yyyy-MM-dd格式的日期'
    },
    after: {
        validator: function (value, param)
        {
            var valueStart = $("input[name ='" + param[0] + "']").val();
            return dateCompare(value, valueStart) <= 0;

        },
        message: '结束日期需要大于等于起始日期'
    },

    integer : {// 验证整数

        validator : function(value) {
                if(value.length>5){}else{
                    return /^[+]?[1-9]+\d*$/i.test(value);
            }

        },
        message : '请输入65535以内的整数'
    },



    CHS: {
        validator: function (value, param)
        {
            var str = "^[\\u0391-\\uFFE5]{" + param[0] + "," + param[1] + "}$";
            var reg = new RegExp(str);
            return reg.test(value);
        },
        message: '请输入{0}-{1}位汉字'
    },
    EN_: {
        validator: function (value, param)
        {
            var str = "^[ A-Za-z0-9_]{" + param[0] + "," + param[1] + "}$";
            var reg = new RegExp(str);
            return reg.test(value);
        },
        message: '请输入{0}-{1}位英文字母、数字、英文标点及下划线'
    },
    CEN_: {
        validator: function (value, param)
        {
            var str = "^[ A-Za-z0-9\\u0391-\\uFFE5_]{" + param[0] + "," + param[1] + "}$";
            var reg = new RegExp(str);
            return reg.test(value);
        },
        message: '请输入{0}-{1}位汉字、英文字母、数字、汉字标点及下划线'
    },
    OX_: {
        validator: function (value, param)
        {
            var str = "^[A-Fa-f0-9]{" + param[0] + "," + param[1] + "}$";
            var reg = new RegExp(str);
            return reg.test(value);
        },
        message: '请输入{0}-{1}位，范围为：A~F、a~f英文字母、0~9数字'
    },
    safepass: {
        validator: function (value, param)
        {
            var str = "^[A-Za-z0-9`\\~!@#\\$%\\^&*\\(\\)\\-=_\\+\\[\\]\\\\\\{\\}\\|;\\':\\\"\\,\\.\\/<>\\?]{" + param[0] + "," + param[1] + "}$";
            var reg = new RegExp(str);
            return reg.test(value);
        },
        message: '密码由{0}-{1}位英文字母、数字及半角标点符号组成'
    },
    equalTo: {
        validator: function (value, param)
        {
            return value == $(param[0]).val();
        },
        message: '两次输入的内容不一致'
    },
    ZIP: {
        validator: function (value, param)
        {
            return /^[1-9]\d{5}$/.test(value);
        },
        message: '邮政编码不存在'
    },
    QQ: {
        validator: function (value, param)
        {
            return /^[1-9]\d{4,10}$/.test(value);
        },
        message: 'QQ号码不正确'
    },
    mobile: {
        validator: function (value, param)
        {
            //return /^(0|86|17951)?(13[0-9]|15[0-9]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(value);
            return /^(0|86|17951)?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9])[0-9]{8}$/.test(value);
            //return /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/.test(value);
            //return /^((\(\d{2,3}\))|(\d{3}\-))?(1[34578]\d{9})$/.test(value);
        },
        message: '手机号码不正确'
    },
    mobileAndEmail: {
        validator: function (value, param) {
            //var mobile = /^((\(\d{2,3}\))|(\d{3}\-))?(1[34578]\d{9})$/;
            var mobile = /^(0|86|17951)?(13[0-9]|15[0-9]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
            var email = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/;
            if (mobile.test(value)) {
                return mobile.test(value);
            } else {
                return email.test(value);
            }
        },
        message: '手机号码/邮箱不正确'
    },
    mobileAndPhone: {
        validator: function (value, param) {
            var mobile = /^(0|86|17951)?(13[0-9]|15[0-9]|17[678]|18[0-9]|14[57])[0-9]{8}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/;
            if (mobile.test(value)) {
                return mobile.test(value);
            }
        },
        message: '输入的手机号码或者电话号码不正确'
    },
    double:{
        validator: function (value, param)
        {
            return /^\d+(\.\d+)?$/.test(value);
        },
        message: '请输入双精度数字'
    },
    number: {
        validator: function (value, param)
        {
            return /^\d+$/.test(value);
        },
        message: '请输入数字'
    },
    idcard: {
        validator: function (value, param)
        {
            return idCard(value);
        },
        message: '请输入正确的身份证号码'
    },
    intRange:{
        validator:function(value,param){

            if(/^[0-9]\d*$/.test(value)){
                return value >= param[0] && value <= param[1];
            }else{
                return false;
            }
        },
        message:'请输入{0}到{1}之间正整数'
    },
    isUrl: {
        validator: function (value, param)
        {
            return checkUrl(value);
        },
        message: '请输入正确的格式的合法链接！（如：https/http/ftp/rtsp/mms://...）'
    }
});


var checkUrl = function (value){
    var strRegex = "^(?:https|http|ftp|rtsp|mms)://"
        + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
        + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
        + "|" // 允许IP和DOMAIN（域名）
        + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
        + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
        + "[a-z]{2,6})" // first level domain- .com or .museum
        + "(:[0-9]{1,4})?" // 端口- :80
        + "((/?)|" // a slash isn't required if there is no file name
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re=new RegExp(strRegex);
    //re.test()
    if (re.test(value)){
        return (true);
    }else{
        return (false);
    }
};

var isDate = function (value)
{
//    return /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/.test(value);
 //原格式验证存在bug，下为修复后的
    return /^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)$/.test(value);

};

var idCard = function (value)
{
    if (value.length == 18 && 18 != value.length)
    {
        return false;
    }
    var number = value.toLowerCase();
    var d, sum = 0, v = '10x98765432', w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = '11,12,13,14,15,21,22,23,31,32,33,34,35,36,37,41,42,43,44,45,46,50,51,52,53,54,61,62,63,64,65,71,81,82,91';
    var re = number.match(/^(\d{2})\d{4}(((\d{2})(\d{2})(\d{2})(\d{3}))|((\d{4})(\d{2})(\d{2})(\d{3}[x\d])))$/);
    if (re == null || a.indexOf(re[1]) < 0)
    {
        return false;
    }
    if (re[2].length == 9)
    {
        number = number.substr(0, 6) + '19' + number.substr(6);
        d = ['19' + re[4], re[5], re[6]].join('-');
    }
    else
    {
        d = [re[9], re[10], re[11]].join('-');
    }
    if (!isDateTime.call(d, 'yyyy-MM-dd'))
    {
        return false;
    }
    for (var i = 0; i < 17; i++)
    {
        sum += number.charAt(i) * w[i];
    }
    return (re[2].length == 9 || number.charAt(17) == v.charAt(sum % 11));
};

var isDateTime = function (format, reObj)
{
    format = format || 'yyyy-MM-dd';
    var input = this, o = {}, d = new Date();
    var f1 = format.split(/[^a-z]+/gi), f2 = input.split(/\D+/g), f3 = format.split(/[a-z]+/gi), f4 = input.split(/\d+/g);
    var len = f1.length, len1 = f3.length;
    if (len != f2.length || len1 != f4.length)
    {
        return false;
    }
    for (var i = 0; i < len1; i++)
    {
        if (f3[i] != f4[i])
        {
            return false;
        }
    }
    for (var i = 0; i < len; i++)
    {
        o[f1[i]] = f2[i];
    }
    o.yyyy = s(o.yyyy, o.yy, d.getFullYear(), 9999, 4);
    o.MM = s(o.MM, o.M, d.getMonth() + 1, 12);
    o.dd = s(o.dd, o.d, d.getDate(), 31);
    o.hh = s(o.hh, o.h, d.getHours(), 24);
    o.mm = s(o.mm, o.m, d.getMinutes());
    o.ss = s(o.ss, o.s, d.getSeconds());
    o.ms = s(o.ms, o.ms, d.getMilliseconds(), 999, 3);
    if (o.yyyy + o.MM + o.dd + o.hh + o.mm + o.ss + o.ms < 0)
    {
        return false;
    }
    if (o.yyyy < 100)
    {
        o.yyyy += (o.yyyy > 30 ? 1900 : 2000);
    }
    d = new Date(o.yyyy, o.MM - 1, o.dd, o.hh, o.mm, o.ss, o.ms);
    var reVal = d.getFullYear() == o.yyyy && d.getMonth() + 1 == o.MM && d.getDate() == o.dd && d.getHours() == o.hh && d.getMinutes() == o.mm && d.getSeconds() == o.ss && d.getMilliseconds() == o.ms;
    return reVal && reObj ? d : reVal;
    function s(s1, s2, s3, s4, s5)
    {
        s4 = s4 || 60, s5 = s5 || 2;
        var reVal = s3;
        if (s1 != undefined && s1 != '' || !isNaN(s1))
        {
            reVal = s1 * 1;
        }
        if (s2 != undefined && s2 != '' && !isNaN(s2))
        {
            reVal = s2 * 1;
        }
        return (reVal == s1 && s1.length != s5 || reVal > s4) ? -10000 : reVal;
    }
};
